#include "obj_orail.h"

int obj_orail::cloaded=0;
GLuint obj_orail::_texture=0;

obj_orail::obj_orail(){
	if (cloaded==0){
		char filename[100];
		sprintf(filename,"./media/objects/obj_orail.tga");
		_texture=load_tga_texture(filename);
		if (_texture==(GLuint)-1){
			debug("Can't find right picture");
			return ;
		}
		cloaded=1;
	}
	texture=_texture;
	z=0;
}

void obj_orail::load(char type){
	verts.resize(4);
	faces.resize(2);
	UVs.resize(4);
	
	verts[0][0]=0;verts[0][1]=-RAIL_WIDTH/2.;verts[0][2]=-0.1;
	verts[1][0]=0;verts[1][1]=+RAIL_WIDTH/2.;verts[1][2]=-0.1;
	verts[2][0]=RAIL_LENGHT;verts[2][1]=+RAIL_WIDTH/2.;verts[2][2]=-0.1;
	verts[3][0]=RAIL_LENGHT;verts[3][1]=-RAIL_WIDTH/2.;verts[3][2]=-0.1;
	
	if (type==0){
		verts[2][0]=RAIL_LENGHT+RAIL_WIDTH/2.*sin(RAIL_ANGLE);
		verts[2][1]=RAIL_WIDTH/2.*cos(RAIL_ANGLE);
		verts[3][0]=RAIL_LENGHT-RAIL_WIDTH/2.*sin(RAIL_ANGLE);
		verts[3][1]=-RAIL_WIDTH/2.*cos(RAIL_ANGLE);
	}
	
	if (type==2){
		verts[2][0]=RAIL_LENGHT-RAIL_WIDTH/2.*sin(RAIL_ANGLE);
		verts[2][1]=RAIL_WIDTH/2.*cos(RAIL_ANGLE);
		verts[3][0]=RAIL_LENGHT+RAIL_WIDTH/2.*sin(RAIL_ANGLE);
		verts[3][1]=-RAIL_WIDTH/2.*cos(RAIL_ANGLE);
	}
	
	faces[0][0]=0;faces[0][1]=1;faces[0][2]=2;faces[0][3]=0;faces[0][4]=1;faces[0][5]=2;
	faces[1][0]=0;faces[1][1]=2;faces[1][2]=3;faces[1][3]=0;faces[1][4]=2;faces[1][5]=3;

	UVs[0][0]=0;UVs[0][1]=0;
	UVs[1][0]=1;UVs[1][1]=0;
	UVs[2][0]=1;UVs[2][1]=1;
	UVs[3][0]=0;UVs[3][1]=1;

	loaded=1;
	transparent=1;
	calc_r();
}

rail obj_orail::next(char type,const rail & cur){
	rail _next;
	_next.x=cur.x + RAIL_LENGHT*cos(cur.rot);
	_next.y=cur.y + RAIL_LENGHT*sin(cur.rot);
	_next.type=type;
	_next.rot=cur.rot + (cur.type-1)*RAIL_ANGLE;
	_next.index=cur.index+1;
	return _next;
}

void obj_orail::draw_rail(const rail & cur){
	x=cur.x;
	y=cur.y;
	rot=cur.rot;
	draw();
}

